package mobi.zeedev.flyingsaucer.common.WebQueryModule.impl;

import com.google.inject.Inject;
import mobi.zeedev.flyingsaucer.common.WebQueryModule.interfaces.IBeerWebQuery;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SingleBeerWebQuery implements IBeerWebQuery {

    @Inject
    public SingleBeerWebQuery() {
        client = new DefaultHttpClient();
    }

    public static final int HTTP_STATUS_OK = 200;

    DefaultHttpClient client;

    @Override
    public void resetCookies() {
        client.getCookieStore().clear();
    }

    @Override
    public void injectCookies(List<Cookie> cookieList) {
        for (Cookie cookie : cookieList) {
            client.getCookieStore().addCookie(cookie);
        }
    }

    @Override
    public Result getUrlResultHttpPost(String url, UrlEncodedFormEntity postParamsIn, CookieStore csIn) throws HttpException, IOException {
//        DefaultHttpClient client = new DefaultHttpClient();

        if (csIn != null) {
            client.setCookieStore(csIn);
        }

        HttpPost postRequest = new HttpPost(url);

        UrlEncodedFormEntity postEntity = postParamsIn;
        if (postEntity != null) {
            postRequest.setEntity(postEntity);
        }

        HttpResponse response = client.execute(postRequest);


        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new HttpException("Invalid response from server: " +
                    status.toString());
        }

        HttpEntity entity = response.getEntity();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        entity.writeTo(content);
        entity.consumeContent();

        Result r = new Result();
//        r.setContent(new String(content.toByteArray()));
        r.setCookies(client.getCookieStore());
        r.setContent(content);
        r.setPostParams(postParamsIn);
        return r;
    }

    @Override
    public Result getUrlResultHttpGet(String url) throws HttpException, IOException {
//        DefaultHttpClient client = new DefaultHttpClient();

//        if (csIn != null) {
//            client.setCookieStore(csIn);
//        }

        HttpGet getRequest = new HttpGet(url);

        HttpResponse response = client.execute(getRequest);


        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new HttpException("Invalid response from server: " +
                    status.toString());
        }

        HttpEntity entity = response.getEntity();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        entity.writeTo(content);
        entity.consumeContent();

        Result r = new Result();
//        r.setContent(new String(content.toByteArray()));
        r.setContent(content);
        r.setCookies(client.getCookieStore());
        //r.postParams = postParamsIn;
        return r;
    }

    @Override
    public UrlEncodedFormEntity generatePostParams(Map<String, String> params) throws UnsupportedEncodingException {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("store", params.get("store")));
        formparams.add(new BasicNameValuePair("card_number", params.get("card_number")));
        formparams.add(new BasicNameValuePair("password", params.get("password")));
        formparams.add(new BasicNameValuePair("f", params.get("f")));
        if (params.get("mou") != null) {
            formparams.add(new BasicNameValuePair("mou", params.get("mou")));
        }
        formparams.add(new BasicNameValuePair("f", params.get("f")));
        UrlEncodedFormEntity postEntity = null;
        postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        return postEntity;

    }

    @Override
    public Result getUrlResultHttpGet(URL url) throws HttpException, IOException {
        return getUrlResultHttpGet(url.toString());
    }


}
